<?php
$isReloadable = false;
require_once "../../../pers/inc.config.php";
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfForm.php';

$oForm = new ovfForm('anagraf');

$sSql = "SELECT ".$oForm->getCampiSelect()." from ovf_anagrafiche A WHERE id_anag = ".reqVarInt('idAnag');
$objDB->query($sSql);
if (!$oForm->aCampiDB = $objDB->fetch()) {
	backOnErr("id. errato '".reqVar('idAnag')."'");
}
if (reqVar('azione') == 'salva') {
	$msgPag = conferma();
} 
if (reqVar('azione') == '') {   
   $aIdAg = $aNomiAge = array();
   $sSql = "SELECT au.id_utente, u.cognome, u.nome FROM ovf_anag_utenti au "
      ." LEFT JOIN ovf_utenti u ON u.id_utente = au.id_utente"
      ." WHERE au.id_anag = ".reqVarInt('idAnag');
   $objDB->query($sSql,'selAge');
   while($acX = $objDB->fetch('selAge')) {
      $aIdAg[] = $acX['id_utente'];
      $aNomiAge[] = $acX['nome']." ".$acX['cognome'];
   }
   reqDef('listIdAge',implode(",",$aIdAg));
   $sListNomiAge = implode(", ",$aNomiAge);
   $oForm->aCampiDB['CALC_nomi_agenti'] = "<span id='listNomiAge'>$sListNomiAge</span>"
      ." &nbsp; (<a href=\"javascript:apriAgentiSelez();\" class='txt1'>seleziona agenti</a>)" ;
	$oForm->db2form();
}
$oForm->preForm();

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>\n";
$sCmdR .= cmdTorna();

testaHtml('Anagrafe','Modifica scheda',$msgPag,'rcom');
?>
<script type="text/javascript">
function apriAgentiSelez() {
   window.open('agentiSelez.php?listIdAge='+document.frm1.listIdAge.value
      +'&xsn=<?=XSN?>','agentiSelez'
      ,'top=200,left=200,height=600,width=700,scrollbars=yes,resizable=yes');
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo  $sCmdR;?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<?=getHtmlHidden('idAnag');?>
<?
echo getHtmlHidden('listIdAge');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Anagrafica</td>
   </tr>
<?
echo $oForm->getHtmlCampiForm();
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         <? echo  $sCmdR;?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma() {
	global $objDB, $objSess, $oForm;
	$msg = '';
	//dbg_($oForm);
	$msg .= $oForm->formCheck();
	
	if ($msg != '') {
		return $msg;
	}
	$oForm->form2db();
	$oForm->campiDB['A_id_anag'] = reqvarInt('idAnag');
	$sSql = $oForm->getUpdateDb("ovf_anagrafiche");
	$objDB->exec($sSql);
	
   
   $sSql = "DELETE FROM ovf_anag_utenti "
      ." WHERE id_anag = ".reqVarInt('idAnag');
   $objDB->exec($sSql);
   
   $aX = explode(',',reqVar('listIdAge'));
   foreach($aX as $idX) {
      $sSql = "INSERT INTO ovf_anag_utenti "
         ." SET id_anag = ".reqVarInt('idAnag').", id_utente = $idX"
         .", data_iniz='2000-01-01', data_fine = '2099-12-31' ";
      $objDB->exec($sSql);
   }

	header("Location: ".urlTorna("anagrafica modificata"));
	exit();
}
